<?php 

session_start();


if(isset($_GET['token']) && !empty($_GET['token'])) {
	$token = str_replace(' ', '+', $_GET['token']);
	$key = 'E9BKL1C4xaZcynCK';
	$token = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($token), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
	$token = json_decode($token);
	
	$_SESSION['userid'] = $token[0]->userid;
	$_SESSION['username'] = $token[0]->username;
	
	// set local user_id to interact with local site
	include_once $_SERVER['DOCUMENT_ROOT'].'/application/library/Betting_lib.php';
	$_SESSION['local_userid'] 	= Betting_lib::get_userid($token[0]->username,'value');
	
	header('Location: index.php');
} 

?>

<!DOCTYPE html>
<html>
	<head>
		<title>Login 2Bet</title>
		<link rel="stylesheet" type="text/css" href="css/style.css" />
		<script src="js/modernizr.custom.63321.js"></script>
		<link href="css/main.css" media="all" rel="stylesheet" type="text/css" />
	</head>
	<body>
		
		<div class="container">
			<br><br><br><br>
			<header>
				<h1>Login <strong>2Bet</strong> betting</h1>
				<h2>Your online realtime product betting service</h2>
				<div class="support-note">
					<span class="note-ie">Sorry, only modern browsers.</span>
				</div>	
			</header>
			<section class="main">
				<div class="errormessage">
					<?php 
						if($_SESSION['error-message']) {
							echo $_SESSION['error-message']; 
							$_SESSION['error-message'] = NULL;
						}
						if($_SESSION['new-user-data']) {
							echo $_SESSION['new-user-data'];
							$_SESSION['new-user-data'] = NULL;	
						}
						if($_SESSION['netsdata']) {
							$t =  $_SESSION['netsdata'];
							print_r($t);
							$_SESSION['netsdata'] = NULL;
						}
						if($_SESSION['new_wallet']) {
							echo $_SESSION['new_wallet'];
							$_SESSION['new_wallet'] = NULL;	
						}
						if($_SESSION['data']) {
							$a = $_SESSION['data'];
							echo '<pre>';
							print_r($a);
							$_SESSION['data'] = NULL;	
						}
						if($_SESSION['token']){
							$c = $_SESSION['token'];
							echo '<pre>';
							print_r($c);
							$_SESSION['token'] = NULL;
						}
						if($_SESSION['cookie_value']) {
							$b = $_SESSION['cookie_value'];
							echo '<pre>';
							print_r($b);
							$_SESSION['cookie_value'] = NULL;	
						}
						//echo '<pre>';
						//print_r($_COOKIE); 
					?>
				</div>
				<form class="form-1" method="POST" action="login-auth.php">
					<p class="field">
						<input type="text" name="username" placeholder="Username">
						<i class="icon-user icon-large"></i>
					</p>
						<p class="field">
							<input type="password" name="password" placeholder="Password">
							<i class="icon-lock icon-large"></i>
					</p>
					<p class="submit">
						<button type="submit" name="submit"><i class="icon-arrow-right icon-large"></i></button>
					</p>
				</form>
			</section>
			<div class="login">
				<div class="new_user">
					<br><br><br> 	
					<h4><a href="newuser.php">New user?</a></h4>
				</div>
			</div>
			<br>
			<div class="login_externale">
				<a href="http://davidilag.dk/2Login/">Login externaly to create a SSO token</a>
			</div>
			
        </div>

	</body>
</html>